package com.ytjj.qmyx.admin.controller;

import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.annotation.OperLog;
import com.ytjj.qmyx.admin.constants.SysLogConstants;
import com.ytjj.qmyx.admin.model.request.LiveProductRequest;
import com.ytjj.qmyx.admin.model.response.LiveProductResponse;
import com.ytjj.qmyx.admin.service.LiveProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/liveProduct")
public class LiveProductController {

    @Autowired
    private LiveProductService liveProductService;

    @PostMapping("/list")
    @OperLog(operModule = "直播商品管理模块", operDesc = "直播商品列表", operType = SysLogConstants.OPERATION_SELECT)
    public CommonResult getLiveList(@RequestBody LiveProductRequest liveProductRequest){
        CommonPage<LiveProductResponse> page = liveProductService.getLiveProductList(liveProductRequest);
        return CommonResult.success(page);
    }

    @PostMapping("/insertLiveProduct")
    @OperLog(operModule = "直播商品管理模块", operDesc = "增加直播商品", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult insertLiveProduct(@RequestBody LiveProductRequest liveProductRequest) {
        int count = liveProductService.insertLiveProduct(liveProductRequest);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @PostMapping("/updateStatus")
    @OperLog(operModule = "直播商品管理模块", operDesc = "修改直播商品上下架状态", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateStatus(@RequestBody LiveProductRequest liveProductRequest) {
        int count = liveProductService.updateStatus(liveProductRequest.getId(), liveProductRequest.getStatus());
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @GetMapping("/deleteLiveProduct")
    @OperLog(operModule = "直播商品管理模块", operDesc = "删除直播间的直播商品", operType = SysLogConstants.OPERATION_DELETE)
    public CommonResult deleteLiveProduct(@RequestParam Integer id) {
        int count = liveProductService.deleteLiveProduct(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @PostMapping("/updateIsTop")
    @OperLog(operModule = "直播商品管理模块", operDesc = "修改直播商品置顶状态", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateIsTop(@RequestBody LiveProductRequest liveProductRequest) {
        int count = liveProductService.updateIsTop(liveProductRequest.getId(), liveProductRequest.getLiveId(), liveProductRequest.getIsTop());
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @PostMapping("/updateSort")
    @OperLog(operModule = "直播商品管理模块", operDesc = "修改直播商品排序", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateSort(@RequestBody LiveProductRequest liveProductRequest) {
        int count = liveProductService.updateSort(liveProductRequest.getId(), liveProductRequest.getSort());
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

}
